package com.woniuxy.joylifecenter.service.impl;


import com.woniuxy.common.constant.ForbiddenWordsConstant;
import com.woniuxy.common.entity.CenterForbiddenwords;
import com.woniuxy.common.entity.CenterForbiddenwordsExample;
import com.woniuxy.common.mapper.CenterForbiddenwordsMapper;
import com.woniuxy.joylifecenter.exception.CenterException;
import com.woniuxy.joylifecenter.service.ForbiddenWordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ForbiddenWordsServiceImpl implements ForbiddenWordsService {
    @Autowired
    CenterForbiddenwordsMapper centerForbiddenwordsMapper;

    /**
     * @return java.lang.Integer
     * @Author ArmWatt
     * @Date 2020/5/11
     * @Param [words]
     * @Description 审核禁词
     **/
    @Override
    public Integer checkWords(List<String> words) {
        CenterForbiddenwordsExample example = new CenterForbiddenwordsExample();
        CenterForbiddenwordsExample.Criteria criteria = example.createCriteria();
        criteria.andCenterFibwordStatusEqualTo(new Byte("1"));
        criteria.andCenterKeywordIn(words);
        int count = centerForbiddenwordsMapper.selectByExample(example).size();
        if (count == 0) {
            //如果禁词表匹配结果为0，则返回通过审核
            return ForbiddenWordsConstant.PASS;
        } else {
            //否则，返回审核不通过
            return ForbiddenWordsConstant.REJECT;
        }

    }

    /**
     * @return java.util.List<java.lang.String>
     * @Author ArmWatt
     * @Date 2020/5/11
     * @Param [words]
     * @Description 批量添加禁词
     **/
    @Override
    public List<String> batchAddWords(List<CenterForbiddenwords> words) {
        List<CenterForbiddenwords> existWords = addVerify(words);
        if (existWords.size() == 0) {
            centerForbiddenwordsMapper.batchInsertWords(words);
            return null;
        } else {//如果查重结果不为空，则返回对应的重复禁词集合
            List<String> wordsTemp = new ArrayList<String>();
            for (CenterForbiddenwords word : existWords) {
                wordsTemp.add(word.getCenterKeyword());
            }
            return wordsTemp;
        }

    }


    /**
     * @param word
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/11
     * @Param [word]
     * @Description 按条件模糊查询禁词
     */
    @Override
    public List<CenterForbiddenwords> queryWordsByCondition(CenterForbiddenwords word) {
        CenterForbiddenwordsExample example = new CenterForbiddenwordsExample();
        CenterForbiddenwordsExample.Criteria criteria = example.createCriteria();
        if ((word.getCenterFibwordid() != null)&&(!word.getCenterFibwordid().equals(""))) {
            criteria.andCenterFibwordidLike("%" + word.getCenterFibwordid() + "%");
        }
        if ((word.getCenterKeyword() != null)&&(!word.getCenterKeyword().equals(""))) {
            criteria.andCenterKeywordLike("%" + word.getCenterKeyword() + "%");
        }
        if ((word.getCenterAddtime() != null)&&(!word.getCenterAddtime().equals(""))) {
            criteria.andCenterAddtimeGreaterThanOrEqualTo(word.getCenterAddtime());
        }
        if ((word.getCenterFibwordStatus() != null)&&(!word.getCenterFibwordStatus().equals(""))) {
            criteria.andCenterFibwordStatusEqualTo(word.getCenterFibwordStatus());
        }
        return centerForbiddenwordsMapper.selectByExample(example);

    }

    /**
     * @param words
     * @return java.util.List<com.woniuxy.common.entity.CenterForbiddenwords>
     * @Author ArmWatt
     * @Date 2020/5/11
     * @Param [words]
     * @Description 添加查重
     */
    @Override
    public List<CenterForbiddenwords> addVerify(List<CenterForbiddenwords> words) {
        CenterForbiddenwordsExample example = new CenterForbiddenwordsExample();
        CenterForbiddenwordsExample.Criteria criteria = example.createCriteria();
        List<String> wordsTemp = new ArrayList<String>();
        for (CenterForbiddenwords word : words) {
            wordsTemp.add(word.getCenterKeyword());
        }
        criteria.andCenterKeywordIn(wordsTemp);
        return centerForbiddenwordsMapper.selectByExample(example);
    }

    /**
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/11
     * @Param [wordsId]
     * @Description 批量删除禁词
     **/
    @Override
    public int batchDeleteWords(List<String> wordsId) {
        CenterForbiddenwordsExample example = new CenterForbiddenwordsExample();
        CenterForbiddenwordsExample.Criteria criteria = example.createCriteria();
        criteria.andCenterFibwordidIn(wordsId);
        return centerForbiddenwordsMapper.deleteByExample(example);
    }

    /**
     * @param word
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/11
     * @Param [word]
     * @Description 编辑单个禁词
     */
    @Override
    public int editWord(CenterForbiddenwords word) {
        CenterForbiddenwordsExample example = new CenterForbiddenwordsExample();
        CenterForbiddenwordsExample.Criteria criteria = example.createCriteria();
        criteria.andCenterFibwordidNotEqualTo(word.getCenterFibwordid());
        criteria.andCenterKeywordEqualTo(word.getCenterKeyword());
        criteria.andCenterFibwordStatusEqualTo(word.getCenterFibwordStatus());
        //编辑查重结果为0则更新记录
        if (centerForbiddenwordsMapper.selectByExample(example).size() == 0) {
            return centerForbiddenwordsMapper.updateByPrimaryKeySelective(word);
        } else {
            throw new CenterException("修改失败，该禁词已存在");
        }


    }

}
